﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace Vs2010LoadTestPluginLib
{
    [DisplayName("Compare context parameter values")]
    [Description("Execute the statement if both parameters have the same value (compared using ToString() and ==)")]
    public class ContextParamsCompare : ConditionalRule
    {
        [DisplayName("Context parameter 1 name")]
        [Description("an existing context parameter")]
        public string ContextParam1 { get; set; }

        [DisplayName("Context parameter 2 name")]
        [Description("an existing context parameter")]
        public string ContextParam2 { get; set; }

        public override void CheckCondition(object sender, ConditionalEventArgs e)
        {
            object val1 = e.WebTest.Context[ContextParam1];
            object val2 = e.WebTest.Context[ContextParam2];

            e.IsMet = val1 == val2;
            if (!e.IsMet && val1 != null)
                e.IsMet = val1.ToString() == val2.ToString();

            if (!e.IsMet)
                e.Message = String.Format("Different, first:'{0}', second:'{1}'", val1, val2);
        }

        public override string StringRepresentation()
        {
            return String.Format("Both context parameters {0} and {1} have the same value", ContextParam1, ContextParam2);
        }
    }
}
